package com.wangziyu.facerecognitiongate.task.base

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

/**
 * @description
 * @author wzy
 * @date 2025/08/10 02:19:53
 */
abstract class BaseTask(override val name: String) : Task {
    private val _statusFlow = MutableStateFlow(TaskStatus.IDLE)
    override val statusFlow: StateFlow<TaskStatus> get() = _statusFlow

    override suspend fun execute() {
        _statusFlow.value = TaskStatus.START
        try {
            doWork()
        } catch (e: Exception) {
            _statusFlow.value = TaskStatus.EXCEPTION
            throw e
        } finally {
            _statusFlow.value = TaskStatus.STOP
        }
    }

    protected abstract suspend fun doWork()
}